﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ACWinAPI;

namespace MuScreens
{
    public partial class FormMain : Form
    {
        private User32.RECT OldWinRECT;
        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            if (Screen.AllScreens.Length <= 1)
            {
                MessageBox.Show("没有检测到多个屏幕,程序即将退出！", "警告");
            }
            InitScreen();
            InitSeleScreen();
            InitWindows();

            if (cb_AppWindow.Items.Count > 0)
            {
                cb_AppWindow.SelectedIndex = 0;
            }
            
        }

        private void cb_Screen_DropDown(object sender, EventArgs e)
        {
            InitScreen();
        }

        private void cb_AppWindow_DropDown(object sender, EventArgs e)
        {
            InitWindows();
        }

        /// <summary>
        /// 初始化选择框
        /// </summary>
        private void InitScreen()
        {
            cb_Screen.DataSource = null;
            //获取所有屏幕
            cb_Screen.DataSource = Screen.AllScreens;
            cb_Screen.DisplayMember = "DeviceName";
        }

        /// <summary>
        /// 初始化选择非主屏
        /// </summary>
        private void InitSeleScreen()
        {
            //选择非现在系统设置的主屏幕
            foreach (Screen item in cb_Screen.Items)
            {
                if (item != Screen.PrimaryScreen)
                {
                    cb_Screen.SelectedItem = item;
                    break;
                }
            }
        }
        /// <summary>
        /// 初始化显示所有窗口
        /// </summary>
        private void InitWindows()
        {
            cb_AppWindow.Items.Clear();
            User32.EnumWindows(delegate (IntPtr hWnd, IntPtr lParam)
            {
                //定义变量
                StringBuilder sb = new StringBuilder(256);
                IntPtr hWnd_tmp = hWnd;
                //检测窗口如果没有显示则跳过
                if (!User32.IsWindowVisible(hWnd)) return true;
                //获取窗口的标题
                User32.GetWindowText(hWnd, sb, sb.Capacity);
                //如果标题没空则跳过
                if (string.IsNullOrWhiteSpace(sb.ToString())) return true;
                //设置列表内容
                cb_AppWindow.Items.Add(sb.ToString());
                return true;

            }, (IntPtr)0);
        }

        private void bt_Run_Click(object sender, EventArgs e)
        {
            //获取选择的屏幕
            Screen sTmp = (Screen)cb_Screen.SelectedItem;

            //获取窗口句柄
            IntPtr hWnd = User32.FindWindow(null, cb_AppWindow.Text);

            //保存当前窗口位置
            User32.GetWindowRect(hWnd, ref OldWinRECT);

            //设置任务栏隐藏
            if (cb_HideBar.Checked)
            {
                User32.SetWindowLongPtr(hWnd, (int)User32.WindowLongFlags.GWL_EXSTYLE, (IntPtr)User32.WindowExStyles.WS_EX_TOOLWINDOW);
            }

            //设置窗口位置(暂时不设置是否最大化)
            User32.SetWindowPos(hWnd, (IntPtr)User32.HwndZOrder.HWND_TOPMOST, 
                sTmp.Bounds.X, sTmp.Bounds.Y, sTmp.Bounds.Width, sTmp.Bounds.Height, 
                User32.WindowPositionFlags.SWP_SHOWWINDOW);

            //最大化显示窗口
            User32.ShowWindow(hWnd, User32.ShowWindowCommands.SW_MAXIMIZE);


        }

        private void bt_Reset_Click(object sender, EventArgs e)
        {
            //暂时只返回到主屏幕

            //获取窗口句柄
            IntPtr hWnd = User32.FindWindow(null, cb_AppWindow.Text);

            //恢复任务栏可见
            User32.SetWindowLongPtr(hWnd, (int)User32.WindowLongFlags.GWL_EXSTYLE, (IntPtr)User32.WindowExStyles.WS_EX_APPWINDOW);

            //恢复窗口
            User32.ShowWindow(hWnd, User32.ShowWindowCommands.SW_SHOWDEFAULT);
            User32.SetWindowPos(hWnd, (IntPtr)User32.HwndZOrder.HWND_NOTOPMOST,
                OldWinRECT.Left, OldWinRECT.Top, OldWinRECT.Rect.Width, OldWinRECT.Rect.Height,
                User32.WindowPositionFlags.SWP_SHOWWINDOW);


        }

        
    }
}
